package org.com.myfb.serviceimpl;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;


import org.com.myfb.dao.StudentsDaoInter;

import org.com.myfb.model.Students;
import org.com.myfb.service.StudentsServiceInter;
import org.com.myfb.tool.ToolInter;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class StudentsServiceImpl implements StudentsServiceInter {

	private StudentsDaoInter studentsDaoImpl;
	private ToolInter ToolImpl;
	
	
	public ToolInter getToolImpl() {
		return ToolImpl;
	}


	public void setToolImpl(ToolInter toolImpl) {
		ToolImpl = toolImpl;
	}


	public StudentsDaoInter getStudentsDaoImpl() {
		return studentsDaoImpl;
	}


	public void setStudentsDaoImpl(StudentsDaoInter studentsDaoImpl) {
		this.studentsDaoImpl = studentsDaoImpl;
	}


	@Override
	public boolean updateStudents(Students student, String sname, String ssex, String sclass, Date sstarttime,
			int sxuezhi) {
		// TODO Auto-generated method stub
		student.setSname(sname);
		student.setSsex(ssex);
		student.setSclass(sclass);
		student.setSstarttime(sstarttime);
		student.setSxuezhi(sxuezhi);
		this.getStudentsDaoImpl().update(student);
		return true;
	}


	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public String querryStudents() {
	
		List<Students> students = this.getStudentsDaoImpl().queryAll();
		
		JSONArray jsonArray = new JSONArray();
		
		JSONObject obj = new JSONObject();
		
		JSONArray practiceReport = new JSONArray();
		
		for(int i = 0;i < students.size();i ++){
			
			LinkedHashMap report = new LinkedHashMap();
			try {
				
				report.put("sId", students.get(i).getSid());
				
				report.put("sName", students.get(i).getSname());
				
				report.put("sSex", students.get(i).getSsex());
				
				report.put("sClass", students.get(i).getSclass());
			
				
				report.put("sStarttime", this.getToolImpl().dateToString1(students.get(i).getSstarttime()));
				
				report.put("sXuezhi", students.get(i).getSxuezhi());
				
				JSONObject json = JSONObject.fromObject(report);
			
				practiceReport.add(json);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		obj.put("data", practiceReport);
		jsonArray.add(obj);
		return jsonArray.toString();
	}


	@SuppressWarnings("unchecked")
	@Override
	public String queryStudentByName(String where) {
		// TODO Auto-generated method stub
		
		List<Students> student = this.getStudentsDaoImpl().findList(Students.class, where);
		JSONArray jsonArray = new JSONArray();
		JSONObject obj = new JSONObject();
		JSONArray  student1 = new JSONArray();
		
		for(int i=0;i<student.size();i++){
			LinkedHashMap stu = new LinkedHashMap();
			try{
				
			   stu.put("sId",student.get(i).getSid());
			   stu.put("sName",student.get(i).getSname());
			   stu.put("sSex",student.get(i).getSsex());  
			   stu.put("sClass", student.get(i).getSclass());
			   stu.put("sStarttime", this.getToolImpl().dateToString1(student.get(i).getSstarttime()));
			   stu.put("sXuezhi", student.get(i).getSxuezhi());
				JSONObject json = JSONObject.fromObject(stu);
				student1.add(json);
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		obj.put("data", student1);
		jsonArray.add(obj);
		return jsonArray.toString();
	}
		
}




